// pages/index/index.js

Page({
  data:{
    categoryId:1,
    schoolListInfo:[],
    imgServer: getApp().globalData.imgServer,
  },
   //分享功能
   onShareAppMessage:function(){
    return {
      title: '油菜花',
      desc: '油菜花',
      path: '/page/school/list'
    }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数

    this.loadSchollList({categoryId: options.categoryId,pageNum:1, pageSize: 10});
   
  },
  
  onReady:function(){
    // 页面渲染完成
   
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  changeCategory: function (e){
    
    this.setData({ categoryId: e.target.dataset.categoryid });
    //TODO 在这里加载数据
    
  },
  swiperChangeCategory: function(e){
    var categoryId = e.detail.current + 1;
    this.setData({
      categoryId: categoryId
    });
    this.loadSchollList({ categoryId: categoryId, pageNum: 1, pageSize: 10 });
  },
  openSchoolHomeWindow:function(e){
    console.log(e.currentTarget.dataset.schoolid);
     wx.navigateTo({
       url: '/page/school/detail?schoolid=' + e.currentTarget.dataset.schoolid,
     })
  },
  loadSchollList: function (requestObj){
    var self = this;
    wx.request({
      url: getApp().globalData.server + "/university/schoolList.do",
      data: requestObj,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.status != 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'fail',
            duration: 1000,
            mask: true
          })
        }
        else {
          //@kate 返回的数据中添加 图片服务器的地址字段
          //res.data.data.list.imgServer = getApp().globalData.imgServer;
          var schoolData = res.data.data;
          var latitude = 0;
          var longitude = 0;
          wx.getLocation({
            type: 'wgs84',
            success: function (res) {

              latitude = res.latitude
              longitude = res.longitude
              console.log(latitude + "<===>" + longitude);
              for (var key in schoolData.list) {
                //学校经度：schoolList[key].address.latitude
                //学校维度:schoolList[key].address.longitude
                //当前经度：
                //当前维度:
                var oldLatitude = parseFloat(schoolData.list[key].address.latitude);
       
                var oldLongitude = parseFloat(schoolData.list[key].address.longitude);
                console.log(latitude + "===" + longitude + "===" + oldLatitude + "===" + oldLongitude);
                var distance = self.getFlatternDistance(latitude, longitude, oldLatitude, oldLongitude);

                //console.log(typeof distance);
                schoolData.list[key].address.distance = Math.floor((distance / 1000)*100)/100;
                //console.log(schoolList[key].address);
              }
              console.log("================================")
              console.log(schoolData);
              console.log("================================")
              self.setData({
                schoolListInfo: schoolData
              });
         
            }
          });
          
        }

      },
      fail: function () {
        wx.showToast({
          title: '获取学校数据失败',
          icon: 'fail',
          duration: 1000,
          mask: true
        })
      }
    })  
  },
  getRad:function(d) {
    return d *(getApp().globalData.PI) / 180.0;
  },
  getFlatternDistance:function(lat1, lng1, lat2, lng2){
 
    var f = this.getRad((lat1 + lat2) / 2);
    var g = this.getRad((lat1 - lat2) / 2);
    var l = this.getRad((lng1 - lng2) / 2);

    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);

    var s, c, w, r, d, h1, h2;
    var a = getApp().globalData.EARTH_RADIUS;
    var fl = 1 / 298.257;

    sg = sg * sg;
    sl = sl * sl;
    sf = sf * sf;

    s = sg * (1 - sl) + (1 - sf) * sl;
    c = (1 - sg) * (1 - sl) + sf * sl;

    w = Math.atan(Math.sqrt(s / c));
    r = Math.sqrt(s * c) / w;
    d = 2 * w * a;
    h1 = (3 * r - 1) / 2 / c;
    h2 = (3 * r + 1) / 2 / s;
    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  }

})